/**
 * (c) Copyright Ascensio System SIA 2025
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hulman.oms.office.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hulman.oms.util.DateUtil;
import com.onlyoffice.model.documenteditor.Callback;
import com.onlyoffice.service.documenteditor.callback.CallbackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/office")
@Slf4j
public class CallbackController
{
    @Autowired
    private CallbackService callbackService;
    @Autowired
    private HttpServletRequest request;

    private final ObjectMapper mapper = new ObjectMapper();

    @PostMapping("/callback")
    public Object callback(@RequestBody final Callback callback, String fileId)
    {
        Map<String, Object> result = new HashMap<>();
        try
        {
            log.info("Callback object: {}", mapper.writeValueAsString(callback));
            //Callback verifiedCallback = callbackService.verifyCallback(callback, null);
            callbackService.processCallback(callback, fileId);
            result.put("error", 0);
        } catch (Exception ex)
        {
            log.error("Callback error:", ex);
            result.put("error", 1);
        }
        return result;
    }

}
